package com.linkage.jz.bankruptcy.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.base.BaseActivity;
import com.linkage.jz.bankruptcy.bean.TaskAtta;
import com.linkage.jz.bankruptcy.utils.FileUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.Collections;
import java.util.List;

/**
 * @author AFAP
 * 文件列表呈现
 */
public class FileAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Activity mActivity;
    private List<TaskAtta> mData;
    private LayoutInflater mInflater;

    public GridLayoutManager createLayoutManager() {
            GridLayoutManager gridLayoutManager = new GridLayoutManager(mActivity, 4, LinearLayoutManager.VERTICAL, false);
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int i) {
                    switch (getItemViewType(i)) {
                        case 1:
                            return 1;
                        case 0:
                        default:
                            return 4;
                    }
                }
            });
            return gridLayoutManager;
    }


    public static void sorFiles(List<TaskAtta> fileList, boolean imgFirst) {
        // 排序，图片在下
        Collections.sort(fileList, (o1, o2) -> {
            if (FileUtil.isImage(o1.getAttachmentName()) && !FileUtil.isImage(o2.getAttachmentName())) {
                return imgFirst ? -1 : 1;
            } else if (!FileUtil.isImage(o1.getAttachmentName()) && FileUtil.isImage(o2.getAttachmentName())) {
                return imgFirst ? 1 : -1;
            }
            return 0;
        });
    }


    public FileAdapter(Activity activity, List<TaskAtta> list) {
        mActivity = activity;
        mData = list;
        mInflater = LayoutInflater.from(activity);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int type) {
        if (type == 1) {
            View v = mInflater.inflate(R.layout.activity_pro_public_image_item, viewGroup, false);
            return new ImageViewHolder(v);
        } else {
            View v = mInflater.inflate(R.layout.activity_pro_public_file_item, viewGroup, false);
            return new FileViewHolder(v);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (FileUtil.isImage(mData.get(position).getAttachmentName())) {
            return 1;
        }
        return 0;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int i) {
        TaskAtta atta = mData.get(i);
        String url = atta.getAttachmentWatermarkAddress();
        if (TextUtils.isEmpty(url)) {
            url = atta.getAttachmentPreviewAddress();
        }
        if (TextUtils.isEmpty(url)) {
            url = atta.getAttachmentDownloadAddress();
        }

        if (holder instanceof ImageViewHolder) {
            ImageLoader.getInstance().displayImage(url, ((ImageViewHolder) holder).img_file);
            ((ImageViewHolder) holder).img_file.setOnClickListener(v -> BaseActivity.openOrDownloadFile(atta,
                    mActivity));
        } else if (holder instanceof FileViewHolder) {
            ((FileViewHolder) holder).tv_file_name.setText(atta.getAttachmentName());

            ((FileViewHolder) holder).img_file_type.setOnClickListener(v -> BaseActivity.openOrDownloadFile(atta,
                    mActivity));
            ((FileViewHolder) holder).tv_file_name.setOnClickListener(v -> BaseActivity.openOrDownloadFile(atta,
                    mActivity));
        }
    }


    @Override
    public int getItemCount() {
        return mData.size();
    }

    class FileViewHolder extends RecyclerView.ViewHolder {
        TextView tv_file_name;
        ImageView img_file_type;

        FileViewHolder(View itemView) {
            super(itemView);
            tv_file_name = itemView.findViewById(R.id.tv_file_name);
            img_file_type = itemView.findViewById(R.id.img_file_type);
        }
    }

    class ImageViewHolder extends RecyclerView.ViewHolder {
        ImageView img_file;

        ImageViewHolder(View itemView) {
            super(itemView);
            img_file = itemView.findViewById(R.id.img_file);
        }
    }
}
